/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License. You may obtain a copy of the License at.
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 *  either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

package com.mansoor.app.islamicnameslite.activities;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.mansoor.app.islamicnameslite.adapters.FavoriteAdapter;
import com.mansoor.app.islamicnameslite.beans.Name;
import com.mansoor.app.islamicnameslite.db.NamesDao;

import java.util.List;

import static com.mansoor.app.islamicnameslite.util.AppConstant.DELETE_ID;
import static com.mansoor.app.islamicnameslite.util.AppConstant.PROGRESS;

/**
 * Created by IntelliJ IDEA.
 * User: TKMAQ61
 * Date: Oct 21, 2009
 * Time: 3:12:57 PM
 */
public class ViewFavorates extends ListActivity
{

    private List<Name> names = null;
    private NamesDao dao;

    private final Handler handler = new Handler()
    {
        public void handleMessage(final Message msg)
        {

            dismissDialog(PROGRESS);
            setListAdapter(new FavoriteAdapter(getBaseContext(), names));
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        final ListView listView = getListView();
        listView.setOnCreateContextMenuListener(this);
        listView.setEmptyView(new TextView(this));

        dao = new NamesDao(this);
        showDialog(PROGRESS);
        ServiceThread t = new ServiceThread();
        t.start();
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = null;
        switch (id)
        {
            case PROGRESS:
                ProgressDialog progressDialog = new ProgressDialog(ViewFavorates.this);
                // progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("Fetching");
                progressDialog.setMessage("Favorates");
                progressDialog.setIndeterminate(true);
                dialog = progressDialog;
        }

        return dialog;

    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
    {
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);    //To change body of overridden methods use File | Settings | File Templates.
        contextMenu.add(0, DELETE_ID, 0, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem menuItem)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();

        if (menuItem.getItemId() == DELETE_ID)
        {
            Name name = names.get(info.position);
            dao.delete(name.getName());
           

            names.remove(info.position);
            FavoriteAdapter adapter = (FavoriteAdapter) getListView().getAdapter();
            adapter.notifyDataSetChanged();

            return true;
        }
        else
        {
            return super.onContextItemSelected(menuItem);    //To change body of overridden methods use File | Settings | File Templates.
        }
    }


    private class ServiceThread extends Thread
    {
        @Override
        public void run()
        {
            super.run();    //To change body of overridden methods use File | Settings | File Templates.
            try
            {
                names = dao.retrieveFavorate();

                handler.sendEmptyMessage(0);
            }
            catch (Exception e)
            {
                dismissDialog(PROGRESS);

            }

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            dao.cleanup();

        }

        return super.onKeyDown(keyCode, event);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
